package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.bo.goods.GoodsSkuBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.enums.goods.GoodsStatusEnum;
import com.ymk.core.enums.goods.GoodsTypeEnum;
import com.ymk.mq.MessageSender;
import com.ymk.service.IGoodsService;
import com.ymk.service.IGoodsSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;

@Service
public class GoodsPostCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO goodsBO = super.getData(sign, GoodsBO.class);
        goodsBO.setType(GoodsTypeEnum.SIMPLE.getType());
        goodsBO.setStatus(GoodsStatusEnum.DISABLED.getStatus());
        super.setModifiedData(sign,goodsBO);
        this.goodsService.post(goodsBO);

        GoodsSkuBO goodsSkuBO = new GoodsSkuBO();
        goodsSkuBO.setGoodsId(goodsBO.getId());
        goodsSkuBO.setMchId(goodsBO.getMchId());
        goodsSkuBO.setPropId("");
        goodsSkuBO.setPropName("默认规格");
        goodsSkuBO.setCostPrice(goodsBO.getCostPrice());
        goodsSkuBO.setPrice(goodsBO.getPrice());
        goodsSkuBO.setSalePrice(goodsBO.getSalePrice());
        goodsSkuBO.setStatus(GoodsStatusEnum.NORMAL.getStatus());
        super.setCreatedData(sign,goodsSkuBO);
        this.goodsSkuService.post(goodsSkuBO);

        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO goodsBO = super.getData(sign, GoodsBO.class);

        goodsBO.setType(GoodsTypeEnum.SIMPLE.getType());
        goodsBO.setStatus(GoodsStatusEnum.DISABLED.getStatus());
        super.setCreatedData(sign,goodsBO);
        this.goodsService.post(goodsBO);

        GoodsSkuBO goodsSkuBO = new GoodsSkuBO();
        goodsSkuBO.setGoodsId(goodsBO.getId());
        goodsSkuBO.setMchId(sign.getMchId());
        goodsSkuBO.setPropId("");
        goodsSkuBO.setPropName("默认规格");
        goodsSkuBO.setCostPrice(goodsBO.getCostPrice());
        goodsSkuBO.setPrice(goodsBO.getPrice());
        goodsSkuBO.setSalePrice(goodsBO.getSalePrice());
        goodsSkuBO.setStatus(GoodsStatusEnum.NORMAL.getStatus());
        super.setCreatedData(sign,goodsSkuBO);
        this.goodsSkuService.post(goodsSkuBO);

        this.messageSender.sendGoodsCacheSync(goodsBO.getId(),sign);
        this.messageSender.sendGoodsCatCacheSync(sign.getMchId(),sign);

        goodsBO.setGoodsSkuBOList(Collections.singletonList(goodsSkuBO));
        respSign.setSource(super.getSource(goodsBO));
        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {

        return null;
    }

    @Override
    public String getCmdCode() {
        return GoodsCmdCode.GOODS_POST;
    }

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsSkuService goodsSkuService;
    @Autowired
    private MessageSender messageSender;
}